package com.gr.staffpm.widget.jqgrid.column;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;

import com.gr.staffpm.widget.jqgrid.EditableGrid;
import com.gr.staffpm.widget.jqgrid.component.RemoveItemLink;
import com.gr.staffpm.widget.jqgrid.component.SaveItemLink;

public abstract class EditableGridActionsPanel<T> extends Panel {

    private static final long serialVersionUID = 1L;

    protected abstract void onSave(T object, AjaxRequestTarget target);

    protected abstract void onError(AjaxRequestTarget target);

    protected abstract void onCancel(AjaxRequestTarget target);

    protected abstract void onDelete(T object, AjaxRequestTarget target);

    public EditableGridActionsPanel(String id, final Item cellItem, final EditableGrid<T> dataTable) {
        super(id);

        final ListItem rowItem = cellItem.findParent(ListItem.class);

        AjaxLink editLink = new AjaxLink("edit") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                dataTable.setSelectedItem(rowItem);
                target.addComponent(dataTable);
            }

            @Override
            public boolean isVisible() {
                return !isThisRowBeingEdited(rowItem, dataTable);
            }
        };
        AjaxLink cancelLink = new AjaxLink("cancel") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                dataTable.setSelectedItem(null);
                target.addComponent(dataTable);
                onCancel(target);
            }

            @Override
            public boolean isVisible() {
                return isThisRowBeingEdited(rowItem, dataTable);
            }
        };
        @SuppressWarnings("unchecked")
        AjaxLink<T> deleteLink = new RemoveItemLink<T>("delete", rowItem) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onRemove(T object, AjaxRequestTarget target) {
                dataTable.setSelectedItem(null);
                target.addComponent(dataTable);
                onDelete(object, target);
            }
        };

        AjaxSubmitLink saveLink = new SaveItemLink<T>("save", rowItem) {

            private static final long serialVersionUID = 1L;

            @Override
            public boolean isVisible() {
                return isThisRowBeingEdited(rowItem, dataTable);
            }

            @Override
            protected void onSuccess(AjaxRequestTarget target) {
                dataTable.setSelectedItem(null);
                target.addComponent(dataTable);
                List<T> list = getListView().getList();
                int index = rowItem.getIndex();
                if (index != -1) {
                    T obj = list.get(index);
                    onSave(obj, target);
                }
            }

            @Override
            protected void onError(AjaxRequestTarget target) {
                EditableGridActionsPanel.this.onError(target);
            }

        };

        add(editLink);
        add(saveLink);
        add(cancelLink);
        add(deleteLink);
    }

    private boolean inEditingMode(EditableGrid<T> dataTable) {
        return dataTable.getSelectedItem() != null;
    }

    private boolean isThisRowBeingEdited(ListItem rowItem, EditableGrid<T> dataTable) {
        return inEditingMode(dataTable) && rowItem.equals(dataTable.getSelectedItem());
    }
}
